﻿using MediatR;
using Newtonsoft.Json;
using Warehouse.Domain;
using Warehouse.Infrstructre.Imp;
using Warehouse.Read.API.Applcation.Command.InventoryCommand;
using static WMSpublic.ParseAPI;
using WMSpublic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using NPOI.XSSF.UserModel;
using Warehouse.Infrstructre.Interface;
using Warehouse.Domain.DTO;

namespace Warehouse.Read.API.Applcation.CommandHandlers.InventoryCommandHandler
{
    /// <summary>
    /// 添加时显示产品中介者
    /// </summary>
    public class SelectInventoryofproductsCommandHandler : IRequestHandler<SelectInventoryofproductsCommand, List<InventProductsSetDTO>>
    {
        private readonly IInventProductRepository _inventProduct;

        public SelectInventoryofproductsCommandHandler(IInventProductRepository inventProduct)
        {
            _inventProduct = inventProduct;
        }

        ///获取入库产品id，库位表、入库产品详情表、产品表
        public async Task<List<InventProductsSetDTO>> Handle(SelectInventoryofproductsCommand request, CancellationToken cancellationToken)
        {
            List<StoreHouse> houses = await GetStoreInfo(1, 1000);//库位表
            List<Products> products = await GetProductsInfo(1, 1000);//产品表
            List<StockProducts> stocks = await GetStockProducts(1, 1000);//入库产品详情表
            List<InventProductsSetDTO> list = new List<InventProductsSetDTO>();
            foreach (var item in request.ids)
            {
                var res = from cp in products
                          join rkcp in stocks on cp.ProductsID equals rkcp.ProductsID
                          join kw in houses on rkcp.Binlocation equals kw.StorehouseId
                          //join ip in await _inventProduct.GetAllAsyns() on cp.ProductsID equals ip.ProductId
                          where rkcp.StockProductId == item
                          select new InventProductsSetDTO
                          {
                              //InventProductIdId=ip.InventProductIdId,
                              //InventoryId=ip.InventoryId,
                              //InventoryNumber=ip.InventoryNumber,
                              //Number=ip.Number,
                              Quantity =rkcp.Quantity,
                              ProductId = rkcp.ProductsID,
                              ProductName = cp.ProductsName,
                              ProductCode = cp.ProductsCode,
                              Specifications = cp.Specs,
                              Batch = rkcp.Batch,
                              Unit = rkcp.Batch,
                              WarehouseLocation = kw.StorehouseName,
                              StockProductId = rkcp.StockProductId
                          };
                list.AddRange(res);
            }
            return list.ToList();




        }
        /// <summary>
        /// 获取库位信息
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetStoreInfo(int pageindex, int pagesize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIzIiwic2lkIjoiMTg1OTgwMzUwNjYzMDg1NjcwNCIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvZ3JvdXBzaWQiOiIxLDEsMSwxIiwianRpIjoiNDMzMDY2ZGYtZjI2MC00MjY3LWJhZjktYmQzODFkZWFkYzBiIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6IuWNouiKsyIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL3VyaSI6Ii8iLCJpc3MiOiJSZW5hb2JvMjAwNCIsImF1ZCI6IldlaWZ1d3UyMjAzQSJ9.sWP96tbSYf5jz3reVpvWkhrkHgM2U5YkHFmwk4E7_V0";
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = "http://116.205.121.197:8000/ckd/api/ReadStorehouse/GetStorehouseList?pageIndex=" + pageindex + "&PageSize=" + pagesize;
            string postRep = await client.GetAsync(url);
            var vendors = JsonConvert.DeserializeObject<ApiResultList<StoreHouse>>(postRep);
            return vendors.data.list;
        }

        /// <summary>
        /// 库位表
        /// </summary>
        public class StoreHouse : Audit
        {
            /// <summary>
            /// 主键id
            /// </summary>
            [Key]
            public int StorehouseId { get; set; }
            /// <summary>
            /// 库位编号
            /// </summary>
            public string? StorehouseCode { get; set; }
            /// <summary>
            /// 库位名称
            /// </summary>
            public string? StorehouseName { get; set; }
            /// <summary>
            /// 库位类型
            /// </summary>
            public string? StorehouseType { get; set; }
            /// <summary>
            /// 仓库id
            /// </summary>
            public int? WarehouseId { get; set; }
            /// <summary>
            /// 是否禁用
            /// </summary>
            public bool? IsDisable { get; set; }
            /// <summary>
            /// 是否默认
            /// </summary>
            public bool? IsDefaults { get; set; }
            /// <summary>
            /// 创建时间
            /// </summary>
            public DateTime? CreatTime { get; set; }
            /// <summary>
            /// 备注
            /// </summary>
            public string? Sremark { get; set; }
            /// <summary>
            /// 公司id
            /// </summary>
            public string? office_id { get; set; }
            /// <summary>
            /// 用户id
            /// </summary>
            public int? user_id { get; set; }

        }
        /// <summary>
        /// 获取产品信息
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetProductsInfo(int pageindex, int pagesize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIzIiwic2lkIjoiMTg1OTgwMzUwNjYzMDg1NjcwNCIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvZ3JvdXBzaWQiOiIxLDEsMSwxIiwianRpIjoiNDMzMDY2ZGYtZjI2MC00MjY3LWJhZjktYmQzODFkZWFkYzBiIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6IuWNouiKsyIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL3VyaSI6Ii8iLCJpc3MiOiJSZW5hb2JvMjAwNCIsImF1ZCI6IldlaWZ1d3UyMjAzQSJ9.sWP96tbSYf5jz3reVpvWkhrkHgM2U5YkHFmwk4E7_V0";
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = "http://116.205.121.197:8000/cpd/api/ProductRead/GetAllProductInfo?pageindex=" + pageindex + "&pagesize=" + pagesize;
            string postRep = await client.GetAsync(url);
            var vendors = JsonConvert.DeserializeObject<ApiResultList<Products>>(postRep);
            return vendors.data.list;
        }
        /// <summary>
        /// 产品管理
        /// </summary>
        public class Products : Audit
        {
            /// <summary>
            /// 产品id
            /// </summary>
            [Key]
            public int ProductsID { get; set; }
            /// <summary>
            /// 产品编号
            /// </summary>
            public string? ProductsCode { get; set; }
            /// <summary>
            /// 产品名称
            /// </summary>
            public string? ProductsName { get; set; }
            /// <summary>
            /// 厂商编码
            /// </summary>
            public string? VendorsCode { get; set; }
            /// <summary>
            /// 内部编码
            /// </summary>
            public string? InteriorCode { get; set; }
            /// <summary>
            /// 规格
            /// </summary>
            public string? Specs { get; set; }
            /// <summary>
            /// 产品类别
            /// </summary>
            public int? ProductTypeID { get; set; }
            /// <summary>
            /// 存储单位
            /// </summary>
            public string? StorageUnits { get; set; }
            /// <summary>
            /// 预警（上）
            /// </summary>
            public int? Upperlimit { get; set; }
            /// <summary>
            /// 预警（下）
            /// </summary>
            public int? Lowerlimit { get; set; }
            /// <summary>
            /// 包装类型
            /// </summary>
            public int? WrapType { get; set; }
            /// <summary>
            /// 价格
            /// </summary>
            public decimal? Price { get; set; }
            /// <summary>
            /// 重量
            /// </summary>
            public decimal? Weight { get; set; }
            /// <summary>
            /// 默认仓库
            /// </summary>
            public int? WarehouseId { get; set; }
            /// <summary>
            /// 库位
            /// </summary>
            public int? StorehouseId { get; set; }
            /// <summary>
            /// 默认供应商
            /// </summary>
            public int? VendorId { get; set; }
            /// <summary>
            /// 默认客户
            /// </summary>
            public int? ClientId { get; set; }
            /// <summary>
            /// 描述
            /// </summary>
            public string? Describe { get; set; }
            /// <summary>
            /// 产品数量
            /// </summary>
            public int? ProcurementNum { get; set; }
            /// <summary>
            /// 公司id
            /// </summary>
            public string? Office_Id { get; set; }

            /// <summary>
            /// 用户id
            /// </summary>
            public int? User_Id { get; set; }
        }
        /// <summary>
        /// 入库产品关联信息表
        /// </summary>
        public class StockProducts
        {
            /// <summary>
            /// 入库产品id
            /// </summary>
            [Key]
            public int StockProductId { get; set; }
            /// <summary>
            /// 入库id
            /// </summary>
            public int? StockId { get; set; }
            /// <summary>
            /// 产品id
            /// </summary>
            public int? ProductsID { get; set; }
            /// <summary>
            /// 批次
            /// </summary>
            public string? Batch { get; set; }
            /// <summary>
            /// 单价
            /// </summary>
            public decimal? Sprice { get; set; }
            /// <summary>
            /// 入库数
            /// </summary>
            public int? Quantity { get; set; }
            /// <summary>
            /// 库位
            /// </summary>
            public int? Binlocation { get; set; }
            /// <summary>
            /// 状态
            /// </summary>
            public int? StateSP { get; set; }
            /// <summary>
            /// 审核人
            /// </summary>
            public string? ShenHePeople { get; set; }
            /// <summary>
            /// 审核时间
            /// </summary>
            public DateTime? ShenHeTime { get; set; }
            /// <summary>
            /// 公司id
            /// </summary>
            public string? office_id { get; set; }
            /// <summary>
            /// 用户id
            /// </summary>
            public int? user_id { get; set; }

        }

        /// <summary>
        /// 获取入库产品信息
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetStockProducts(int pageindex, int pagesize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIzIiwic2lkIjoiMTg1OTgwMzUwNjYzMDg1NjcwNCIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvZ3JvdXBzaWQiOiIxLDEsMSwxIiwianRpIjoiNDMzMDY2ZGYtZjI2MC00MjY3LWJhZjktYmQzODFkZWFkYzBiIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6IuWNouiKsyIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL3VyaSI6Ii8iLCJpc3MiOiJSZW5hb2JvMjAwNCIsImF1ZCI6IldlaWZ1d3UyMjAzQSJ9.sWP96tbSYf5jz3reVpvWkhrkHgM2U5YkHFmwk4E7_V0";

            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = "http://116.205.121.197:8000/ckd/api/EnterStorages/GetWarehouseList?PageIndex=" + pageindex + "&PageSize=" + pagesize;
            string postRep = await client.GetAsync(url);
            var vendors = JsonConvert.DeserializeObject<ApiResultList<StockProducts>>(postRep);
            return vendors.data.list;
        }






    }
}
